<template>
  <div class="password-reset-container">
    <div class="password-reset-box">
      <div class="password-reset-header">
        <el-icon
          :size="48"
          color="#1890ff"
        >
          <Lock />
        </el-icon>
        <h1>重置密码</h1>
        <p>请输入您的邮箱和新密码</p>
      </div>

      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        size="large"
        @submit.prevent="handleReset"
      >
        <el-form-item prop="email">
          <el-input
            v-model="formData.email"
            placeholder="请输入邮箱"
            prefix-icon="Message"
          />
        </el-form-item>

        <el-form-item prop="new_password">
          <el-input
            v-model="formData.new_password"
            type="password"
            placeholder="请输入新密码（至少8位）"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item prop="confirm_password">
          <el-input
            v-model="formData.confirm_password"
            type="password"
            placeholder="请再次输入新密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleReset"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            style="width: 100%"
            @click="handleReset"
          >
            重置密码
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-button
            style="width: 100%"
            @click="goBack"
          >
            返回登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
defineOptions({ name: 'PasswordResetPage' })
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthApi } from '@/api/modules/auth'
import type { FormInstance, FormRules } from 'element-plus'
import notify from '@/utils/notify'

const router = useRouter()
const authApi = useAuthApi()

// 表单数据
const formData = reactive({
  email: '',
  new_password: '',
  confirm_password: '',
})

// 自定义验证：确认密码与新密码一致
const validateConfirmPassword = (_rule: unknown, value: string, callback: (error?: Error) => void) => {
  if (value === '') {
    callback(new Error('请再次输入新密码'))
  } else if (value !== formData.new_password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules: FormRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' },
  ],
  new_password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 8, message: '密码长度不能少于8位', trigger: 'blur' },
  ],
  confirm_password: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' },
  ],
}

const formRef = ref<FormInstance>()
const loading = ref(false)

// 重置密码处理
const handleReset = async () => {
  if (!formRef.value) return

  await formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    loading.value = true
    try {
      await authApi.resetPasswordDirect({
        email: formData.email,
        new_password: formData.new_password,
        confirm_password: formData.confirm_password,
      })
      notify.success('密码重置成功，请使用新密码登录')
      router.push('/login')
    } catch (error: unknown) {
      const axiosError = error as { response?: { data?: { detail?: string } } }
      notify.error(axiosError.response?.data?.detail || '密码重置失败')
    } finally {
      loading.value = false
    }
  })
}

// 返回登录页
const goBack = () => {
  router.push('/login')
}
</script>

<style scoped>
.password-reset-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.password-reset-box {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.password-reset-header {
  text-align: center;
  margin-bottom: 40px;
}

.password-reset-header h1 {
  margin: 16px 0 8px;
  font-size: 24px;
  color: #333;
}

.password-reset-header p {
  margin: 0;
  font-size: 14px;
  color: #999;
}
</style>
